<!DOCTYPE html>
<html>
<head>
    <title>用户</title>
    <#include "/common/import.ftl" >
    <script type="text/javascript" src="${ctx }/js/jquery-md5-min.js"></script>
    <script>
        var pluginsSwitch = "${pluginsSwitch!''}";

        $.extend($.fn.validatebox.defaults.rules, {
            /*必须和某个字段相等*/
            equalTo: {
                validator: function (value, param) {
                    return $(param[0]).val() == value;
                },
                message: '字段不匹配'
            }

        });

        var userId = '${item.userId!""}';
        $(function () {
            if ("1" == pluginsSwitch && "" != '${item.orgId!""}') {
                $("#orgId").prop("disabled", true);
            }

            $('#inputForm').form({
                onSubmit: function () {
                    var pass = $("#userPassInput").val();
                    var pass2 = $("#userPass2").val();
                    var radioVal = $("input[name*='upass']:checked").val();
                    var email = $('#email').val();
                    if (radioVal == undefined || radioVal == 'Y') {
                        var flag = $(this).form('validate');
                        if (!flag) {
                            return flag;
                        }
                        if ($.trim(pass).length == 0) {
                            $.messager.alert('错误提示', '请输入正确密码!', 'error');
                            flag = false;
                        }
                        if ($.trim(pass2).length == 0) {
                            $.messager.alert('错误提示', '请输入正确的确认密码!', 'error');
                            flag = false;
                        }
                        if ($.trim(pass) != $.trim(pass2)) {
                            $.messager.alert('错误提示', '两次输入密码不一致!', 'error');
                            flag = false;
                        }
                        if (flag) {
                            $("#md5Pass").val(pass.md5());
                        }
                        return flag;
                    } else if (radioVal == 'N') {
                        $('#userPassInput').validatebox({required: false});
                        $('#userPass2').validatebox({required: false});
                        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                        if (!myreg.test(email)) {
                            $.messager.alert('错误提示', '邮箱格式不正确!', 'error');
                            flag = false;
                            return flag;
                        }
                    }
                    return $(this).form('validate');
                },
                success: function (data) {
                    var result = jQuery.parseJSON(data);
                    if (result.flag == 'T') {
                        $.messager.alert('提交结果', '操作成功', 'info', function () {
                            parent.closeUserAdd();
                        });
                        parent.reload();
                    } else {
                        $.messager.alert('提交结果', '操作失败:' + result.msg, 'error');
                    }
                }
            });
            <#if item.userId??>
               $("#userEname").attr("readonly", true);
                uPssOrN('N');
            </#if>
        });

        function uPssOrN(flag) {
            if (flag == 'Y') {
                $('#userPassInput').validatebox({required: true});
                $('#userPass2').validatebox({required: true});
                $('.isupdPass').show();
            } else if (flag == 'N') {
                $('#userPassInput').validatebox({required: false});
                $('#userPass2').validatebox({required: false});
                $('.isupdPass').hide();
            }
        }
    </script>


    <script language=javascript>
        //判断输入密码的类型
        function CharMode(iN) {
            if (iN >= 48 && iN <= 57) //数字
                return 1;
            if (iN >= 65 && iN <= 90) //大写
                return 2;
            if (iN >= 97 && iN <= 122) //小写
                return 4;
            else
                return 8;
        }

        //bitTotal函数
        //计算密码模式
        function bitTotal(num) {
            modes = 0;
            for (i = 0; i < 4; i++) {
                if (num & 1) modes++;
                num >>>= 1;
            }
            return modes;
        }

        //返回强度级别
        function checkStrong(sPW) {
            if (sPW.length <= 4)
                return 0; //密码太短
            Modes = 0;
            for (i = 0; i < sPW.length; i++) {
//密码模式  
                Modes |= CharMode(sPW.charCodeAt(i));
            }
            return bitTotal(Modes);
        }

        //显示颜色
        function pwStrength(pwd) {
            O_color = "#eeeeee";
            L_color = "#FF0000";
            M_color = "#FF9900";
            H_color = "#33CC00";
            if (pwd == null || pwd == '') {
                Lcolor = Mcolor = Hcolor = O_color;
            }
            else {
                S_level = checkStrong(pwd);
                switch (S_level) {
                    case 0:
                        Lcolor = Mcolor = Hcolor = O_color;
                    case 1:
                        Lcolor = L_color;
                        Mcolor = Hcolor = O_color;
                        break;
                    case 2:
                        Lcolor = Mcolor = M_color;
                        Hcolor = O_color;
                        break;
                    default:
                        Lcolor = Mcolor = Hcolor = H_color;
                }
            }
            document.getElementById("strength_L").style.background = Lcolor;
            document.getElementById("strength_M").style.background = Mcolor;
            document.getElementById("strength_H").style.background = Hcolor;
            return;
        }
    </script>

<body>

<div id="tip">
    <form id="inputForm" name="inputForm" method="post" action="save.action">
        <table class="datagrid-body">
            <input type="hidden" name="formToken" id="formToken" value="${formToken!''}">
            <input type="hidden" name="userId" id="userId" value="${item.userId!''}"/>
            <input type="hidden" name="md5Pass" id="md5Pass" value=""/>
            <input type="hidden" name="oldOrgId" id="oldOrgId" value="${item.orgId!''}"/>
            <input type="hidden" name="oldDeptId" id="oldDeptId" value="${item.deptId!''}"/>
            <tr>
                <td class="datagrid-header">账号<span style="color:red;">*</span></td>
                <td>

                    <input name="userEname" id="userEname" value="${item.userEname!''}"
                           class="easyui-validatebox" size="30"
                           data-options="required:true,validType:'length[2,30]'"
                           missingMessage="请输入账号" invalidMessage="应为2~30个字"/>
                </td>
            </tr>
            <tr>
                <td class="datagrid-header">姓名<span style="color:red;">*</span></td>
                <td>
                    <input id="userCname" name="userCname" size="30" value="${item.userCname!''}"
                           class="easyui-validatebox"
                           data-options="required:true,validType:'length[2,30]'"
                           missingMessage="请输入姓名" invalidMessage="应为2~30个字"/>
                </td>
            </tr>
            <#if item.userId??>
                <tr>
                    <td class="datagrid-header">是否修改密码</td>
                    <td>
                        <input type="radio" name="upass" id="upass_Y" value="Y"
                               onclick="uPssOrN('Y');"/>是
                        <input type="radio" name="upass" checked="checked" id="upass_N" value="N"
                               onclick="uPssOrN('N');"/>否
                    </td>
                </tr>
            </#if>
            <tr class="isupdPass">
                <td class="datagrid-header">密码<span style="color:red;">*</span></td>
                <td><input id="userPassInput" name="userPassInput" type="password" value=""
                           class="easyui-validatebox" style="width:178px;"
                           data-options="required:true,validType:'length[2,30]'"
                           missingMessage="请输入密码" invalidMessage="密码长度应为2-30位"
                           onKeyUp="pwStrength(this.value)" onBlur="pwStrength(this.value)"/>
                </td>
            </tr>
            <tr class="isupdPass">
                <td class="datagrid-header">密码强度</td>
                <td>
                    <table>
                        <tr>
                            <td width="10%" id="strength_L">弱</td>
                            <td width="10%" id="strength_M">中</td>
                            <td width="10%" id="strength_H">强</td>
                        </tr>
                    </table>


                </td>

            </tr>
            <tr class="isupdPass">
                <td class="datagrid-header">确认密码<span style="color:red;">*</span></td>
                <td><input id="userPass2" name="userPass2" type="password" value=""
                           class="easyui-validatebox" style="width:178px;"
                           data-options="required:true,validType:'length[2,30]'"
                           missingMessage="请确认密码" validType="equalTo['#userPassInput']"
                           invalidMessage="两次输入密码不一致"/>
                </td>
            </tr>
            <tr>
                <td class="datagrid-header">邮箱<span style="color:red;">*</span></td>
                <td>

                    <input name="email" id="email" value="${item.email!''}"
                           class="easyui-validatebox" size="30"
                           data-options="required:true,validType:'email'"
                           missingMessage="请输入邮箱" invalidMessage="请输入正确的邮箱地址"/>
                </td>
            </tr>
            <tr class="Deprecated">
                <td class="datagrid-header">所属机构</td>
                <td>
                    <select id="orgId" name="orgId" style="width:184px;"></select>
                </td>
            </tr>
            <tr class="Deprecated">
                <td class="datagrid-header">用户类型</td>
                <td>
                    <select id="userType" name="userType" style="width:184px;"></select>
                </td>
            </tr>
            <tr class="Deprecated">
                <td class="datagrid-header">所属部门</td>
                <td>
                    <select id="deptId" name="deptId" style="width:184px;"> </select>
                </td>
            </tr>
            <tr class="Deprecated">

                <td class="datagrid-header">上级领导</td>

                <td>

                    <input id="fax" name="fax" value="${item.fax!''}"

                           class="easyui-validatebox" size="30"
                           data-options="validType:'length[2,30]'"

                           invalidMessage="应为2~30个字"/>
                </td>

            </tr>
            <tr>
                <td class="datagrid-header">用户性别</td>
                <td>
                    <input type="radio" name="userSex" id="userSex_M" value="M"/>男
                    <input type="radio" name="userSex" id="userSex_F" value="F"/>女
                </td>
            </tr>
            <tr>
                <td class="datagrid-header">手机<span style="color:red;">*</span></td>
                <td><input id="mobile" name="mobile" value="${item.mobile!''}"
                           class="easyui-validatebox" size="30"
                           data-options="required:true,validType:'mobile'"
                           missingMessage="请输入手机号"/>
                </td>
            </tr>
            <tr>
                <td class="datagrid-header">联系电话</td>
                <td><input id="phone" name="phone" value="${item.phone!''}"
                           class="easyui-validatebox" size="30" data-options="validType:'tel'"
                           missingMessage="请输入联系电话"/>
                </td>
            </tr>
            <tr class="Deprecated">
                <td class="datagrid-header">所在省份</td>
                <td>
                    <select id="province" name="province" style="width:184px;"> </select>
                </td>
            </tr>
            <tr class="Deprecated">
                <td class="datagrid-header">所在地区</td>
                <td>
                    <select id="city" name="city" style="width:184px;"> </select>
                </td>
            </tr>
            <tr>
                <td class="datagrid-header">用户状态</td>
                <td>
                    <input type="radio" name="userStatus" id="userStatus_1" value="1"
                           checked="checked"/>启用
                    <input type="radio" name="userStatus" id="userStatus_0" value="0"/>停用
                </td>
            </tr>
<#if '1'==pluginsSwitch>
    <#if item.userId??><#else>
       <tr>
           <td class="datagrid-header">是否系统用户</td>
           <td>
               <input type="radio" name="delStatus" id="delStatus_1" value="1"/>系统
               <input type="radio" name="delStatus" id="delStatus_0" value="0"
                      checked="checked"/>机构
           </td>
       </tr>
    </#if>
</#if>
            <tr>
                <td class="datagrid-header"></td>
                <td>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-save"
                       onClick="$('#inputForm').submit();">保存</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-save"
                       onClick="parent.closeUserAdd();">关闭</a>
                </td>
            </tr>

        </table>
    </form>
</div>
</body>
<script>
    function initProviCity() {
        //初始化省份列表，注意：fatherValue为固定值 0
        $('#province').combobox({
            //url:'${ctx}/dict/pub/dict-select.action?cateEname=cty&fatherValue=0',
            url: '${ctx}/dict/pub/dict-select.action?cateEname=area&fatherValue=0',
            valueField: 'id',
            editable: false,
            textField: 'text',
            onChange: function () {
                reloadCity();
            }
        });
        //初始化省份列表，注意：fatherValue为初始化时选中的省份的value
        $('#city').combobox({
            url: '${ctx}/dict/pub/dict-select.action?cateEname=area&fatherValue=${item.province!""}',
            valueField: 'id',
            textField: 'text',
            editable: false
        });
    }

    //根据选中的省份，动态构造城市下拉列表
    function reloadCity() {
        var provinceTmp = $('#province').combobox('getValue');
        $('#city').combobox('clear');
        if (provinceTmp.length > 0) {
            var cityUrlTmp = '${ctx}/dict/pub/dict-select.action?cateEname=area&fatherValue=' + provinceTmp;
            $('#city').combobox('reload', cityUrlTmp);
        } else {
            $('#city').combobox('loadData', []);
        }
    }

    function initOrg() {
        //初始化组织pluginsSwitch
        var url = "${ctx}/uct/org/org!initOrg.action";
        if ("1" == pluginsSwitch && "1" != "${sysUser!''}") {
            url = url + "?orgId=${loginUser.user.orgId}";
        }
        $('#orgId').combobox({
            url: url,
            valueField: 'orgId',
            textField: 'orgCname',
            onChange: function () {
                reloadDept();
            }
        });
    }

    function initDept() {
        //初始化部门
        $('#deptId').combobox({
            url: '${ctx}/uct/dept/dept!initDept.action?orgId=${item.orgId!""}',
            valueField: 'deptId',
            textField: 'deptCname'
        });
    }

    function reloadDept() {
        $('#deptId').combobox('setValue', '');
        var orgId = $('#orgId').combobox('getValue');
        var deptUrl = '${ctx}/uct/dept/dept!initDept.action?orgId=' + orgId;
        $('#deptId').combobox('reload', deptUrl);
    }

    function initUserType() {
        $('#userType').combobox({
            url: '${ctx}/dict/pub/dict-select.action?cateEname=memberType',
            value: '${item.userType!""}',
            valueField: 'id',
            textField: 'text'
        });
    }

    $(function () {
        initOrg();
        initDept();
        initUserType();
        initProviCity();

        initSelect("userType", "${item.userType!''}");
        initRadio("userSex", "${item.userSex!''}");
        initRadio("userStatus", "${item.userStatus!''}");

        if ($("#userId").val().length > 0) {
            $('#orgId').combobox('setValue', '${item.orgId!""}');
        } else {
            $('#orgId').combobox('setValue', '${loginUser.user.orgId}');
            if ("1" == pluginsSwitch) {
                initRadio("delStatus", "0");
            }
        }

        $('#deptId').combobox('setValue', '${item.deptId!""}');
        $('#province').combobox('setValue', '${item.province!""}');
        $('#city').combobox('setValue', '${item.city!""}');
    });
</script>
</html>